package com.ruoyi.system.service.aggregation.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.aggregation.Account;
import com.ruoyi.system.domain.aggregation.Withdraw;
import com.ruoyi.system.mapper.aggregation.AccountMapper;
import com.ruoyi.system.mapper.aggregation.WithdrawMapper;
import com.ruoyi.system.service.aggregation.IWithdrawService;

/**
 * 提现Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@Service
public class WithdrawServiceImpl implements IWithdrawService 
{
    @Autowired
    private WithdrawMapper withdrawMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Withdraw selectWithdrawById(Long id)
    {
        return withdrawMapper.selectWithdrawById(id);
    }

    @Override
    public List<Withdraw> selectWithdrawList(Withdraw withdraw)
    {
        return withdrawMapper.selectWithdrawList(withdraw);
    }

    /**
     * 提现申请
     */
    @Override
    @Transactional
    public void applyWithdraw(Long userId, Integer userType, Long amount)
    {
        // 查询账户
        Account account = accountMapper.selectAccountByUserId(userId);
        if (account == null)
        {
            throw new ServiceException("账户不存在");
        }

        // 验证可提现余额
        if (account.getBalance() < amount)
        {
            throw new ServiceException("可提现余额不足");
        }

        // 创建提现记录
        Withdraw withdraw = new Withdraw();
        withdraw.setId(System.currentTimeMillis()); // 生成ID
        withdraw.setUserId(userId);
        withdraw.setUserType(userType);
        withdraw.setAmount(amount);
        withdraw.setStatue(1); // 待审核
        withdraw.setDeleted(0);
        withdraw.setCreateTime(new Date());
        withdrawMapper.insertWithdraw(withdraw);

        // 冻结可提现余额（从可提现余额扣除）
        account.setBalance(account.getBalance() - amount);
        account.setUpdateTime(new Date());
        accountMapper.updateAccount(account);
    }

    /**
     * 提现审核
     * statue: 1待审核、2已通过、3已拒绝
     */
    @Override
    @Transactional
    public void checkWithdraw(Long id, Integer statue, String reason)
    {
        Withdraw withdraw = withdrawMapper.selectWithdrawById(id);
        if (withdraw == null)
        {
            throw new ServiceException("提现记录不存在");
        }

        if (withdraw.getStatue() != 1)
        {
            throw new ServiceException("提现状态不正确");
        }

        // 更新提现状态
        withdraw.setStatue(statue);
        withdraw.setReason(reason);
        withdraw.setUpdateTime(new Date());
        withdrawMapper.updateWithdraw(withdraw);

        // 如果拒绝，退回可提现余额
        if (statue == 3)
        {
            Account account = accountMapper.selectAccountByUserId(withdraw.getUserId());
            if (account != null)
            {
                account.setBalance(account.getBalance() + withdraw.getAmount());
                account.setUpdateTime(new Date());
                accountMapper.updateAccount(account);
            }
        }
        // 如果通过，调用支付宝转账
        else if (statue == 2)
        {
            // TODO: 调用支付宝转账接口
            System.out.println("提现审核通过，需要转账金额：" + withdraw.getAmount() + "分");
        }
    }

    @Override
    public int updateWithdraw(Withdraw withdraw)
    {
        withdraw.setUpdateTime(DateUtils.getNowDate());
        return withdrawMapper.updateWithdraw(withdraw);
    }
}

